package com.mm.utils;

import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/17/017.
 */

@ControllerAdvice
public class MyControllerAdvice {
    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Map errorHandler(Exception ex) {
        Map map = new HashMap();
        if (ex instanceof UnauthenticatedException) {
            map.put("code", "1000001");
            map.put("msg", "token错误");
        } else if (ex instanceof com.mm.utils.UnauthorizedException) {
            map.put("code", "1000002");
            map.put("msg", "用户无权限");
        }
        else if (ex instanceof org.apache.shiro.authz.UnauthorizedException) {
            map.put("code", "1000003");
            map.put("msg", "密码错误");
        }else {
            map.put("code", "2000004");
            map.put("msg", ex.getMessage());
        }
        return map;
    }

}
